package com.cookie.meetproject.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.cookie.framework.Framework;
import com.cookie.framework.helper.FlavorHelper;
import com.cookie.framework.utils.LogUtils;

import androidx.multidex.MultiDex;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.base
 * 文件名:    BaseApp
 * 创建者:    cookie
 * 创建时间:   2019/12/31 0:32
 * 描述:      TODO
 */

public class BaseApp extends Application {

    /**
     * Application的优化
     * 1.必要的组件在程序主页去初始化
     * 2.如果组件一定要在APP中初始化那么尽可能延迟
     * 3.非必要组件在子线程初始化
     */
    @Override
    public void onCreate() {
        super.onCreate();
        //只有在主进程中初始化
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))){
           //获取渠道
            String flavor = FlavorHelper.getFlavor(this);
            LogUtils.i("flavor: "+flavor);
            Framework.getFramework().initFramework(this);
        }
        MultiDex.install(this);
    }

    //获取当前进程名
    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess :activityManager.getRunningAppProcesses()){
            if (appProcess.pid==pid){
                return appProcess.processName;
            }
        }
        return null;
    }
}
